﻿package cn.antscript.airCMS.display.component 
{
	import cn.antscript.airCMS.data.PageData;
	import cn.antscript.airCMS.data.XMLData;
	import com.bit101.components.ComboBox;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class Menu extends Sprite
	{
		
		private var _pageBox:ComboBox;
		private var _typeBox:ComboBox;
		private var _groupBox:ComboBox;
		
		public var pageIndex:int;
		public var typeIndex:String;
		public var groupIndex:int;
		
		public var isIntenal:Boolean;
		
		public function Menu() 
		{
			_groupBox = new ComboBox(this, 490, 0);
			_groupBox.setSize(235, 20);
			_groupBox.addEventListener(Event.SELECT, onChange);
			
			_typeBox = new ComboBox(this, 245, 0);
			_typeBox.setSize(235, 20);
			_typeBox.addEventListener(Event.SELECT, onChange);
			
			_pageBox = new ComboBox(this, 0, 0);
			_pageBox.setSize(235, 20);
			_pageBox.addEventListener(Event.SELECT, onChange);
			
			for (var i:int = 0; i < XMLData.getInstance().pagesData.length; i++) 
			{
				_pageBox.addItem( { label:XMLData.getInstance().pagesData[i].label } );
			}
			
			_pageBox.visible = _typeBox.visible = _groupBox.visible = false;
		}
		
		private function onChange(e:Event):void 
		{
			switch(e.target) {
				case _groupBox:
				groupIndex = _groupBox.selectedIndex;
				dispatchEvent(new Event("groupChange"));
				break;
				case _typeBox:
				typeIndex = _typeBox.selectedItem.value;
				dispatchEvent(new Event("typeChange"));
				break;
				case _pageBox:
				pageIndex = _pageBox.selectedIndex;
				dispatchEvent(new Event("pageChange"));
				break;
				default:
				break;
			}
			
		}
		
		
		public function fill(pageInd:int, typeData:TypeContainer=null,typeInd:String="",groupData:Object=null, groupInd:int=-1) {
			_pageBox.visible = true;
			_typeBox.visible = _groupBox.visible = false;
			_pageBox.setSelectedIndex(pageInd);
			
			if (typeData != null) {
				_typeBox.visible = true;
				_typeBox.removeAll();
				for (var i:int = 0,j=0; i < 5; i++) 
				{
					
					if (!typeData.isEmpty[i]) {
						_typeBox.addItem( { label:typeData.typeArr[i], value:typeData.typeStrs[i] } );			
						if (typeData.typeStrs[i] == typeInd) {
							_typeBox.setSelectedIndex(j);
						}
						j++;
					}
					
					
				}
				
			}
			if (groupData != null) {
				_groupBox.visible = true;
				_groupBox.removeAll();
				for (i = 0; i < groupData.length; i++) 
				{
					_groupBox.addItem( { label:groupData[i].label} );
					_groupBox.setSelectedIndex(groupInd);
				}
			}
			
			
			
		}
		
		public function hide():void {
			_pageBox.visible = _typeBox.visible = _groupBox.visible = false;
		}
		
	}

}